﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using System.Threading;

namespace project_assess
{
    public partial class choose_prj : Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        DataTable globetb;
        List<string> strlist;
        public choose_prj(Mfrm frm)
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            mfrm = frm;
            m_db = mfrm.m_db;
            BackgroundWorker back = new BackgroundWorker();
            back.DoWork += new DoWorkEventHandler(back_DoWork);
            this.Load += delegate
            {
                back.RunWorkerAsync();
            };
          
        }

        void back_DoWork(object sender, DoWorkEventArgs e)
        {
            load_suggest();
        }

        void load_suggest()
        {
            strlist = new List<string>();
            string sql = "select [项目名称],[项目编号],[项目经理] from [projects] where [状态]='在建'" +
                " order by [项目编号] desc";
            globetb = m_db.GetDataSet(sql).Tables[0];
            tree.Nodes.Clear();
            for (int i = 0; i < globetb.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = globetb.Rows[i][1].ToString();
                n.Cells.Add(new Cell(globetb.Rows[i][0].ToString()));
                string str = globetb.Rows[i][0].ToString();
                strlist.Add(ChineseConvertor.Convert(str));
                //nameid.AutoCompleteCustomSource.Add(str);
                //prjid.AutoCompleteCustomSource.Add(globetb.Rows[i][1].ToString());
                //manageid.AutoCompleteCustomSource.Add(globetb.Rows[i][2].ToString());
                tree.Nodes.Add(n);
            }
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtpy.Text = "";
        }

        private void index_item_ItemClick(object sender, EventArgs e)
        {
            BaseItem temp_item = (BaseItem)sender;
            string str = temp_item.Text.ToLower();
            txtpy.Text += str;
        }


        //txt变化,然后显示出相应的工程编号
        private void txtpy_TextChanged(object sender, EventArgs e)
        {
            if (txtpy.Text == "")
            {
                tree.Nodes.Clear();
                return;
            }
            tree.Nodes.Clear();
            string txt = txtpy.Text;
            for (int i = 0; i < strlist.Count; i++)
            {
                if (strlist[i].Contains(txt))
                {
                    Node n = new Node();
                    n.Text = globetb.Rows[i][1].ToString();
                    n.Cells.Add(new Cell(globetb.Rows[i][0].ToString()));
                    tree.Nodes.Add(n);
                }
            }

        }



        //双击node选择后
        private void tree_NodeDoubleClick(object sender, TreeNodeMouseEventArgs e)
        {
            Node n = tree.SelectedNode;
            if (n == null)
            {
                return;
            }
            mfrm.curprjid = n.Text;
            mfrm.Text = "监测项目测算系统(当前项目编号:" + n.Text + ")";
            mfrm.msg("已选择工程:" + n.Text);
            this.Close();
        }


        //常规索引的操作
        private void btn_search_Click(object sender, EventArgs e)
        {
            string id = prjid.Text;
            string manager = manageid.Text;
            string name = nameid.Text;
            message_frm frm;
            string sql;//=""
            string sqlid, sqlman, sqlname;
            if (id == "" && manager == "" && name == "")
            {
                frm = new message_frm("没有输入查找项", "错误", icon_img.alert2);
                frm.ShowDialog(this);
                frm.Close();
                return;
            }
            sqlid = "[项目编号] like '%" + id + "%' and ";
            sqlman = "[项目经理] like '%" + manager + "%' and ";
            sqlname = "[项目名称] like '%" + name + "%'";


            sql = "select [项目名称],[项目编号] from [projects] where " + sqlid + sqlman + sqlname;
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = globetb.Rows[i][1].ToString();
                n.Cells.Add(new Cell(globetb.Rows[i][0].ToString()));
                tree.Nodes.Add(n);
            }
        }
    }
}
